<template>
  <el-menu
    default-active="2"
    class="el-menu-vertical-demo"
    background-color="#00A1D6"
    text-color="#fff"
    :collapse="isCollapse"
    active-text-color="#ffd04b"
  >
    <el-menu-item
      :index="item.path"
      v-for="item in noChildren"
      :key="item.path"
      @click="cilkMenu(item)"
    >
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <el-submenu index="index" v-for="(item, index) in hasChildren" :key="index">
      <template slot="title">
        <i class="el-icon-location"></i>
        <span>{{ item.label }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          index="subItem.path"
          v-for="(subItem, subIndex) in item.children"
          :key="subIndex"
          @click="cilkMenu(subItem)"
          >{{ subItem.label }}</el-menu-item
        >
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {
      asideMenu: [
        {
          path: "/",
          label: "首页",
          name: "home",
          icon: "s-home",
        },
        {
          path: "/video",
          label: "视频管理",
          name: "video",
          icon: "video-play",
        },
        {
          path: "/user",
          label: "用户管理",
          name: "user",
          icon: "user",
        },
        {
          label: "其他",
          icon: "user",
          children: [
            {
              path: "/page1",
              label: "页面1",
              name: "page1",
              icon: "user",
            },
            {
              path: "/page2",
              label: "页面2",
              name: "page2",
              icon: "user",
            },
          ],
        },
      ],
    };
  },
  methods: {
    cilkMenu(item) {
      this.$router.push(item.path);
      this.$store.commit("selectMenu", item);
    },
  },
  computed: {
    //过滤渲染菜单栏循环的数据
    noChildren() {
      return this.asideMenu.filter((item) => !item.children);
    },
    hasChildren() {
      return this.asideMenu.filter((item) => item.children);
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-menu {
  height: 100%;
  border: none;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
</style>
